package com.xtgg.more;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.RequestParams;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.UmengRegistrar;
import com.xtgg.R;
import com.xtgg.R.layout;
import com.xtgg.R.menu;
import com.xtgg.device.TextInputActivity;
import com.xtgg.login.LoginActivity;
import com.xtgg.login.SetPasswordActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class ModifyPwdActivity extends BaseActivity implements OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_pwd);

		((TextView) findViewById(R.id.center)).setText("修改密码");
		MobclickAgent.onEvent(this, ((TextView) findViewById(R.id.center))
				.getText().toString());

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.login).setOnClickListener(this);
		findViewById(R.id.other).setOnClickListener(this);
		findViewById(R.id.clear).setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;

		case R.id.login:
			getData();
			break;

		case R.id.clear:
			((EditText) findViewById(R.id.pwd2)).setText("");
			break;
		}
	}

	private void getData() {
		String old = ((EditText) findViewById(R.id.pwd2)).getText().toString();
		String pwd = ((EditText) findViewById(R.id.pwd)).getText().toString();
		final String pwd0 = ((EditText) findViewById(R.id.pwd0)).getText()
				.toString();

		if (pwd.length() < 8 || pwd0.length() < 8 || old.length() < 8) {
			showToast("密码不能少8位");
			return;
		}

		String save = PreferencesUtils.getString(this, "password");
		if (!save.equals(old)) {
			showToast("旧密码输入不正确");
			return;
		}

		if (!pwd.equals(pwd0)) {
			showToast("两次密码输入不一致");
			return;
		}

		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					PreferencesUtils.putString(ModifyPwdActivity.this,
							"password", pwd0);
					showToast("密码修改成功");
					finish();
				}
				else if (ruser.getState().equals("2")) 
				{showExitDialog();}else {
					showToast("" + ruser.getMessage());
				}
			}
		};

		RequestParams params = new RequestParams();
		params.put("act", "resetpwd");
		params.put("mobile", PreferencesUtils.getString(this, "telephone"));
		params.put("password", pwd);
		params.put("oldword", old);
		params.put("device_token", UmengRegistrar.getRegistrationId(this));

		MgqRestClient.get(Constants.USERMGT, params, loginHandler);
	}
}
